﻿/*
 * RegExpress
 * 
 * Copyright (c) 2010, Daniel McGaughran
 * 
 * Licensed under the Apache Licence, Version 2.0 (the "Licence");
 * you may not use this file except in compliance with the Licence.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 * 
 */

using System;
using System.Xml;
using RegExpressModel.Utility;


namespace RegExpressModel
{
	public class WindowSettings : ISettingsSerialiser
	{
		public const int MinWidth = Constants.MinWindowWidth;
		public const int MinHeight = Constants.MinWindowHeight;

		private int _width;
		private int _height;

		private int _maxWidth;
		private int _maxHeight;


		internal WindowSettings(int maxWidth, int maxHeight)
		{
			IsMaximised = false;
			_width = Constants.DefaultWindowWidth;
			_height = Constants.DefaultWindowHeight;

			_maxWidth = Math.Max(maxWidth, MinWidth);
			_maxHeight = Math.Max(maxHeight, MinHeight);
		}



		public bool IsMaximised
		{ get; set; }


		public int Width
		{ 
			get { return _width; }
			set 
			{
				if (value < MinWidth)
					_width = MinWidth;
				else if (value > _maxWidth)
					_width = _maxWidth;
				else
					_width = value;
			} 
		}


		public int Height
		{
			get { return _height; }
			set
			{
				if (value < MinHeight)
					_height = MinHeight;
				else if (value > _maxHeight)
					_height = _maxHeight;
				else
					_height = value;
			}
		}



		public void SaveToXml(XmlDocument targetDocument, XmlElement targetElement)
		{
			XmlElement windowSettingsNode = targetElement.AppendElement(targetDocument, Constants.WindowSettingsNodeName);
			windowSettingsNode.AppendElement(targetDocument, Constants.IsMaximisedNodeName, IsMaximised.ToString());
			windowSettingsNode.AppendElement(targetDocument, Constants.WindowWidthNodeName, Width.ToString());
			windowSettingsNode.AppendElement(targetDocument, Constants.WindowHeightNodeName, Height.ToString());
		}



		public bool ExtractFromXml(XmlElement parentElement)
		{
			string currentValue;
			bool allSettingsLoaded = true;

			bool tempBool;
			currentValue = parentElement.GetTextValue(Constants.IsMaximisedXPath);
			if (Boolean.TryParse(currentValue, out tempBool))
				IsMaximised = tempBool;
			else allSettingsLoaded = false;

			int tempInt;
			currentValue = parentElement.GetTextValue(Constants.WindowWidthXPath);
			if(Int32.TryParse(currentValue, out tempInt))
				Width = tempInt;
			else allSettingsLoaded = false;

			currentValue = parentElement.GetTextValue(Constants.WindowHeightXPath);
			if (Int32.TryParse(currentValue, out tempInt))
				Height = tempInt;
			else allSettingsLoaded = false;

			return allSettingsLoaded;
		}


	}
}
